# -*- encoding:utf-8 -*-
# 开发团队：大数据组
# 开发者：albert·bing
# 开发时间：2020/7/5 20:13
# 文件名称：yellow_calendar.py
# 开发工具：PyCharm


#  start your code

# import sys
# sys.path.append('/home/hadoop/programs/spider/WTP66_BigdataCrawler')
import os
import pandas as pd

def read(dirlen, rootpath, subfiles):
    total_source_list = []
    for i in range(0, dirlen, 1):
        source_far = open(rootpath + subfiles[i], 'r', buffering=1)
        list = source_far.readline()
        total_source_list.append(list)
    return total_source_list


def dircount(paths):
    return len(os.listdir(paths))


def subfilename(path):
    subfiles = []
    for root, dirs, files in os.walk(path):
        subfiles = files
    return subfiles


def filename(path):
    subfiles = []
    for root, dirs, files in os.walk(path):
        # print("当前文件路径:",root)
        if dirs:
            subfiles = dirs
            # print("当前路径下所有子目录:", dirs)
            break
        # print("所有非目录子文件:",files)
    return subfiles


def sortData():
    # 最终结果
    result = []
    # 循环将所有的文件数据读取进来，然后每一个用户的作为一个子数据集合
    # 第二步读取
    reslut_sec = []
    for j in range(1, 6, 1):
        # 根路径
        # rootpath = "E:/KNN1111/GAR/testoutAll_0" + str(1) + "/"
        rootpath = "C:/Users/bing/Desktop/test/KNN1111/GAR/testoutAll_0" + str(1) + "/"
        # 拿到所有子文件的名称
        subfilenames = filename(rootpath)
        # 第一步读取
        reslut_fir = []
        # 获取子文件夹里面的内容
        for i in range(0, len(subfilenames), 1):
            subpath = "C:/Users/bing/Desktop/test/KNN1111/GAR/testoutAll_01/" + subfilenames[i] + "/"
            # subpath = "E:/KNN1111/GAR/testoutAll_01/" + subfilenames[i] + "/"
            dirlen = dircount(subpath)
            subfiles = subfilename(subpath)
            # 返回的数据是一个 S100n 下面的所有的是个或者多个txt文件的集合
            # 代表的是一个用户的第一个结果集的多个数据
            one_list = read(dirlen, subpath, subfiles)
            reslut_fir.append(one_list)
        reslut_sec.append(reslut_fir)

    # 做数据整合
    for j in range(0, 198, 1):
        # 五份数据 枚举
        one_list1 = reslut_sec[0]
        one_list2 = reslut_sec[1]
        one_list3 = reslut_sec[2]
        one_list4 = reslut_sec[3]
        one_list5 = reslut_sec[4]

        one_person_res = []
        # 将一个用户的五份数据放在一个数据中  [[用户的第一份数据],[用户的第二份数据],[。。]]
        for k in range(0, len(one_list1[j]), 1):
            one_result = [one_list1[j][k], one_list2[j][k], one_list3[j][k], one_list4[j][k], one_list5[j][k]]
            one_person_res.append(one_result)
        # 将一个用户的数据放入到最终集合中
        result.append(one_person_res)
    return result


def computeTwoPicMinDistance(list_one, list_two):
    second_list = []
    first_list = []
    for i in range(0, 5, 1):
        for j in range(0, 5, 1):
            first_list.append(computeTwoPointDis(list_one[i], list_two[j]))
        second_list.append(getSumList(first_list))
    return min(second_list)


def getSumList(list):
    sum = 0
    for i in range(0, len(list), 1):
        sum = sum + list[i]
    return sum


def computeTwoPointDis(list1, list2):
    ls1 = list(list1)
    ls2 = list(list2)
    n = len(ls1)
    dis = 0
    for i in range(0, n, 1):
        if ls1[i] == ls2[i]:
            dis = dis + 1

    return dis / 1536


def computeMinDistance(result):
    len_num = len(result)
    result_sum_list = []
    # 198个用户做循环
    for i in range(0, len_num, 1):
        one_sum_list = []
        for j in range(0, len(result[i]), 1):
            for k in range(j,len(result[i]),1):
                if j == k:
                    continue
                else:
                    one_sum_list.append(computeTwoPicMinDistance(result[i][k], result[i][j]))
        print("第[" + str(i) + "]用户计算完成.长度为" + str(len(result_sum_list) + 1))
        print(result_sum_list)
        result_sum_list.append(one_sum_list)

    return result_sum_list


if __name__ == '__main__':
    #  拿到整合好的数据
    result = sortData()

    result_sum_list = computeMinDistance(result)

    sum = []
    for i in range(0,len(result_sum_list),1):
        for j in range(0,len(result_sum_list[i]),1):
            sum.append(result_sum_list[i][j])

    name = ['dis']
    res_csv = pd.DataFrame(columns=name,data=sum)
    res_csv.to_csv('./result_gar.csv',encoding='utf-8')